package com.boyi.cycling_master.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.boyi.cycling_master.R;
import com.boyi.cycling_master.bean.LoginResponseBean;
import com.boyi.cycling_master.bean.ResponseBean;
import com.boyi.cycling_master.common.SnackUtil;
import com.boyi.cycling_master.https.utils.UserRetrofit;
import com.boyi.cycling_master.ui.base.BaseActivity;
import com.rengwuxian.materialedittext.MaterialEditText;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import talon.com.underlineText.UnderLineTextView;

/**
 * @author Lee_Mofeel
 * @version 1.0
 * @date 2017/7/24/024 14:39
 * @Description 注册第二部 确认密码
 */

public class RegisterConfirmPwdActivity extends BaseActivity {
    @BindView(R.id.btn_nav_icon)
    ImageView btnNavIcon;
    @BindView(R.id.edt_register_user_password)
    MaterialEditText edtRegisterUserPassword;
    @BindView(R.id.edt_register_confirm_password)
    MaterialEditText edtRegisterConfirmPassword;
    @BindView(R.id.tv_btn_register)
    TextView tvBtnRegister;
    @BindView(R.id.tv_underline_register)
    UnderLineTextView tvUnderlineRegister;
    // 用户注册的手机号 由上个界面传入
    private String phoneNum;

    @Override
    public int getContentViewId() {
        return R.layout.activity_register_confirm_passwrod;
    }


    @Override
    public void initLogic() {

    }

    @Override
    public void getBundleExtras(Bundle bundle) {
        phoneNum = bundle.getString("phoneNum");
    }


    @OnClick({R.id.btn_nav_icon, R.id.tv_btn_register, R.id.tv_underline_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_nav_icon:// 返回
                finish();
                break;
            case R.id.tv_btn_register:// 注册
                String password = edtRegisterUserPassword.getText().toString();
                String confirm = edtRegisterConfirmPassword.getText().toString();
                if (password.equals(confirm))
                    toFinish(password);// 完成注册
                else
                    SnackUtil.SnackShort(edtRegisterUserPassword, "确保两次密码一致");
                break;
            case R.id.tv_underline_register:
                break;
        }
    }

    /**
     * 完成注册
     * @param password
     */
    public void toFinish(String password) {
        Map<String, String> map = new HashMap<>();
        map.put("userPhone", phoneNum);
        map.put("userPassword", password);
        UserRetrofit.getInstance().toRegister(new Callback<LoginResponseBean>() {
            @Override
            public void onResponse(Call<LoginResponseBean> call, Response<LoginResponseBean> response) {
                LoginResponseBean bean = response.body();
                if (bean == null)
                    return;
                SnackUtil.SnackShort(edtRegisterUserPassword, bean.getResultMessage());
                if (bean.getResultCode().equals("200")) {
                    // 注册成功保存用户信息 跳转到注册信息界面
                    if (bean.getDataCollection().save())
                        startActivity(new Intent(RegisterConfirmPwdActivity.this, RegisterFinishActivity.class));
                }
            }

            @Override
            public void onFailure(Call<LoginResponseBean> call, Throwable t) {

            }
        }, map);

    }
}
